package udpsok

import (
	"errors"
	"fmt"
	"log"
	"net"
	"sync"
	"time"
)

var mutex sync.Mutex
var Clients map[int64]*Client

var Tests map[int64]string

func Run() {
	// 创建监听
	socket, err := net.ListenUDP("udp4", &net.UDPAddr{
		IP:   net.IPv4(0, 0, 0, 0),
		Port: 61005,
	})
	if err != nil {
		fmt.Println("监听失败!", err)
		return
	}
	defer socket.Close()

	Clients = make(map[int64]*Client)
	//Tests[32]="nihao"
	for {
		// 读取数据
		data := make([]byte, 64)
		_, remoteAddr, err := socket.ReadFromUDP(data)
		if err != nil {
			//fmt.Println("读取数据失败!", err)
			continue
		}

		go HandelConn(socket, data, remoteAddr)

		// 发送数据
	}
}
func HandelConn(conn *net.UDPConn, data []byte, addr *net.UDPAddr) {
	defer func() {
		if err:=recover();err!=nil{
			log.Printf("HandelConn err %s\n", err)
		}
	}()
	var sn32 int32
	_,_,_,sn32=ReadHeader(data)
	sn := int64(sn32)

	fmt.Println(data)
	c, ok := Clients[sn]
	if !ok {
		fmt.Println("新建连接!sn", sn, ok)
		mutex.Lock()
		Clients[sn] = NewClient(conn, sn, addr)
		mutex.Unlock()
	} else {
		Clients[sn].UpdateAddr(addr)
		c.rt <- data
	}
}

func OpenDoor(sn int64, num int8, f func(e bool)) {

	msg := &Message{flag: 23, cmd: MSG_OPEN, k: 0, sn: int32(sn), body: &OpenMessage{index: num}}
	mutex.Lock()
	defer mutex.Unlock()
	c, ok := Clients[sn]
	if !ok {
		f(false)
		return
	}
	c.wt <- msg
	c.f = f
}
func HandleOpenDoor(sn int64, num int8) error {
	var channel chan bool
	channel = make(chan bool, 20)
	OpenDoor(sn, num, func(e bool) {
		channel <- e
	})
	var wait chan bool
	wait = make(chan bool, 20)
	go func() {
		time.Sleep(time.Duration(10) * time.Second)
		wait <- false
	}()

	select {
	case status := <-channel:
		if status {
			return nil
		} else {
			return errors.New("请联系管理员")
		}
	case <-wait:
		return errors.New("响应超时")
	}
}
